package com.oba.base.dao;

import com.oba.base.interfaces.IPager;

/**
 * 翻页器
 * 
 * @author han
 * 
 */
public abstract class Pager implements IPager {
	/**
	 * 第一页
	 */
	public void firstPage() {
		if (getCurPage() != 1)
			loadPage(1);
	}

	/**
	 * 最后一页
	 */
	public void lastPage() {
		int pageCount = getPageCount();
		if (getCurPage() != pageCount)
			loadPage(pageCount);
	}

	/**
	 * 下一页
	 */
	public void nextPage() {
		int goPage = getCurPage() + 1;
		if (goPage <= getPageCount())
			loadPage(goPage);
	}

	/**
	 * 前一页
	 */
	public void prePage() {
		int goPage = getCurPage() - 1;
		if (goPage > 0)
			loadPage(goPage);

	}

	/**
	 * 转入指定页
	 */
	public void gotoPage(int page) {
		int pageCount = getPageCount();
		if (page < 1) {
			page = 1;
		} else if (page > pageCount) {
			page = pageCount;
		}
		loadPage(page);
	}

	/**
	 * 获得总页数
	 */
	public int getPageCount() {
		int pageSize = getPageSize();
		if (pageSize == 0)
			return 1;
		int itemCount = getItemCount();
		int pageCount = itemCount / pageSize;
		if (itemCount % pageSize != 0)
			pageCount++;
		if (pageCount == 0)
			pageCount = 1;
		return pageCount;
	}

	/**
	 * 加载指定页
	 */
	protected abstract void loadPage(int page);
}
